import { AppDataSource } from "@/config/database";
import { User } from "@/entities/system/user.entity";
import { Context } from "koa";
import { TokenPayload } from "@/utils/token.util";

export const getProfile = async (ctx: Context) => {
    const { id } = ctx.state.user as TokenPayload;
    
    const userRepository = AppDataSource.getRepository(User);
    const user = await userRepository.findOne({
        where: { id },
    });

    if (!user) {
        ctx.status = 404;
        ctx.body = {
            status: 404,
            message: '用户不存在'
        };
        return;
    }

    ctx.body = {
        status: 200,
        data: {
            id: user.id,
            username: user.username,
            nickname: user.nickname,
            createdAt: user.createdAt
        }
    };
};

/** 校验用户不存在 */